import Vue from 'vue'
import VueRouter from 'vue-router'

import Main from '../views/Main.vue'
import register from '@/user/register'
import  login from "@/user/login";
import 股票市场主体风险预警 from '@/views/股票市场主体风险预警.vue'
import changepwd from "@/user/changepwd";
// import tab from "@/store/tab";


// 解决重复点击页面报NavigationDuplicated: Avoided redundant navigation to current locat
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



Vue.use(VueRouter)


//1.import 创建路由组件
//2.将路由与组件进行映射


const routes = [


  //主路由

  { path:'/',
    component:Main,
    redirect:'/stock_market_entity_risk_warning',//当地址是/时就重定向到股票价格预测分析
    meta : {                      //加一个自定义obj
      requireAuth:true      //这个参数 true 代表需要登录才能进入A
    },
    children:[
      //子路由 - 只保留股票价格预测分析页面(TimesNet)
      {
        path: '/stock_market_entity_risk_warning',
        name: 'stock_market_entity_risk_warning',
        component: 股票市场主体风险预警,
        meta: {
          keepAlive: true,  // 如果需要缓存页面数据
          requireAuth: true,  // 如果该页面需要认证（登录）
          model: 'TimesNet'
        }
      }

    ]

  },
  // 登录
  {path:'/login',name :'login',component: login},

  // 注册
  {path:'/register',name :'register',component: register},
  {path:'/changepwd',name:'changepwd',component: changepwd}
]


//3.创建router实例
const router = new VueRouter({
  routes
})

export default router
export  const isLogin=false;
//4.在main.js挂载


//路由守卫
// router.beforeEach(async(to,from,next) => {
//   const allowedRoutes = ['/systematic_risk', '/market_entities','/individual_risk','/influential_entities'
//     ,'/warning','/risk_warning','/abnormal_detection','/health','/diffus','/pressure','/quotation','/event_analysis',
//     '/event_graph','/knowladgeGraph','/UserManagement','/login','/register','/bigscreen','/test','/sse','/systemrisk',
//     '/importance_measure','/Traceability','/risk_communication'];
//   const name=sessionStorage.getItem('user')
//   // if (allowedRoutes.includes(to.path)) {
//   //   next();
//   // } else {
//   //   next('/'); // 重定向到首页或其他允许的路由
//   // }
//   if (to.meta.requireAuth){//需要认证
//     if (name || !name && (to.name==='/user' || to.name==='/login')){
//       next()
//     }else {
//       next('/login')
//       // return{
//       //   path:'/login',
//       //   query:{redirect:to.fullPath}
//       // }
//     }
//   }
//   else {
//     next()
//   }
// })

// 测试状态下 直接放行
router.beforeEach((to, from, next) => {
  const user = sessionStorage.getItem('user');
  const publicPages = ['/login', '/register']; // 不需要登录的路径
  const authRequired = !publicPages.includes(to.path);

  // if (authRequired && !user) {
  //   return next('/login'); // 没登录又访问私有页 → 跳转登录
  // }

  next(); // 有权限就放行
});


